package com.lq.common.until;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;


/**
 * @author  权哥
 * @date  2023/7/18 19:14
 * @version 1.0
*/

public class Send {

    /**
     *
     * @param subject 邮件标题
     * @param text  邮件内容
     * @return
     */
    public void sendQQEmail(String subject, String text){
        // 邮件服务器配置
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");

        // 发件人和收件人信息
        String fromEmail = "lq404@qq.com";
        String password = "lkobwlczklradefg";
        String toEmail = "3078354639@qq.com";

        // 创建会话
        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, password);
            }
        });

        try {
            // 创建邮件消息
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(fromEmail));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
            message.setSubject(subject);
            message.setText(text);

            // 发送邮件
            Transport.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        new Send().sendQQEmail("测试","test");
    }
}